/*
 *  CELabelMBFontWrapperScissored.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/labels/CELabelMBFontWrapperScissored.h"


using namespace cocos2d;

namespace cocosext {



/////////////////////////////////////////////////////
//
/** CELabelMBFontWrapperScissored 
* 
*/
////////////////////////////////////////////////////

CELabelMBFontWrapperScissored* CELabelMBFontWrapperScissored::node(CELabelBMFont* label, ECETextAllign ta)
{
	CELabelMBFontWrapperScissored *pRet = new CELabelMBFontWrapperScissored(label, ta);
    CCAssert(pRet != NULL, NULL);
    if (pRet->init())
    {
        pRet->autorelease();
        return pRet;
    }
    else
    {
        CC_SAFE_DELETE(pRet)
        return NULL;
    }
}


/////////////////////////////////////////////////////
// Constructor, destructor, init

bool CELabelMBFontWrapperScissored::init()
{
	if (!CENodeRGBA::init())
		return false;

	_frame = CENodeScissored<CCNode>::node();
	_frame->setAnchorPoint(CCPointZero);
	_frame->setContentSize(this->getContentSize());
	_frame->setPosition(CCPointZero);
	this->addChild(_frame, INT_MIN);

	updateLabelPosition();
	_frame->addChild(_label, 0);

	return true;
}


/////////////////////////////////////////////////////
// CELabelProtocol interface
void CELabelMBFontWrapperScissored::setString(const wchar_t *str)
{
	_label->setString(str); 

	if (_ta == eCE_TA_left_to_right)
		updateLeftToRightAllign();
}

/////////////////////////////////////////////////////
// Cocos2d interface
void CELabelMBFontWrapperScissored::setRotation(float fRotation)
{
	CCAssert(false, "CELabelMBFontWrapperScissored::setRotation: CENodeScissored node can't be rotated!");
}

void CELabelMBFontWrapperScissored::setContentSize(const cocos2d::CCSize& contentSize)
{
	CENodeRGBA::setContentSize(contentSize);
	
	if (_frame == NULL)
		return;
	
	_frame->setContentSize(this->getContentSize());
	updateLabelPosition();
}

/////////////////////////////////////////////////////
// Protected interface
void CELabelMBFontWrapperScissored::updateLeftToRightAllign()
{
	if (_label->getContentSize().width*_label->getScaleX() > _frame->getContentSize().width)
	{
		_label->setAnchorPoint(ccp(1.0f, 0.5f));
		_label->setPosition(ccp(_frame->getContentSize().width, _frame->getContentSize().height/2.f));
	}
	else
	{
		_label->setAnchorPoint(ccp(0.f, 0.5f));
		_label->setPosition(ccp(0.f, _frame->getContentSize().height/2.f));
	}
}

void CELabelMBFontWrapperScissored::updateLabelPosition()
{
	switch (_ta)
	{
	case eCE_TA_left:
		{
			_label->setAnchorPoint(ccp(0.f, 0.5f));
			_label->setPosition(ccp(0.f, _frame->getContentSize().height/2.f));
			break;
		}
	case eCE_TA_center:
		{
			_label->setAnchorPoint(ccp(0.5f, 0.5f));
			_label->setPosition(ccp(_frame->getContentSize().width/2.f, _frame->getContentSize().height/2.f));
			break;
		}
	case eCE_TA_right:
		{
			_label->setAnchorPoint(ccp(1.0f, 0.5f));
			_label->setPosition(ccp(_frame->getContentSize().width, _frame->getContentSize().height/2.f));
			break;
		}
	case eCE_TA_left_to_right:
		{
			updateLeftToRightAllign();
			break;
		}
	};
}

/////////////////////////////////////////////////////
//
/** CELabelMBFontWrapScis_cursor 
* 
*/
////////////////////////////////////////////////////

CELabelMBFontWrapScis_cursor* CELabelMBFontWrapScis_cursor::node(CELabelBMFont* label, cocos2d::CCNode* cursor, ECETextAllign ta)
{
	CELabelMBFontWrapScis_cursor *pRet = new CELabelMBFontWrapScis_cursor(label, cursor, ta);
    CCAssert(pRet != NULL, NULL);
    if (pRet->init())
    {
        pRet->autorelease();
        return pRet;
    }
    else
    {
        CC_SAFE_DELETE(pRet)
        return NULL;
    }
}


/////////////////////////////////////////////////////
// Constructor, destructor, init

bool CELabelMBFontWrapScis_cursor::init()
{
	if (!CELabelMBFontWrapperScissored::init())
		return false;

	_frame->addChild(_cursor, INT_MIN);
	updateLabelPosition();

	_cursor->runAction(CCRepeat::actionWithAction(CCSequence::actions(CCHide::action(), 
																	  CCActionInterval::actionWithDuration(CE_LIST_EDIT_CURSOR_HALF_TIME),
																	  CCShow::action(),
																	  CCActionInterval::actionWithDuration(CE_LIST_EDIT_CURSOR_HALF_TIME),
																	  NULL),
																	  INT_MAX));

	return true;
}

/////////////////////////////////////////////////////
// CELabelProtocol interface
void CELabelMBFontWrapScis_cursor::setString(const wchar_t *str)
{
	_label->setString(str); 
	updateLabelPosition();
}

/////////////////////////////////////////////////////
// Protected interface
void CELabelMBFontWrapScis_cursor::updateLeftToRightAllign()
{
	if (_label->getContentSize().width*_label->getScaleX() + _cursor->getContentSize().width*_cursor->getScaleX() > _frame->getContentSize().width)
	{
		_label->setAnchorPoint(ccp(1.0f, 0.5f));
		_label->setPosition(ccp(_frame->getContentSize().width - _cursor->getContentSize().width*_cursor->getScaleX(), _frame->getContentSize().height/2.f));
		_cursor->setAnchorPoint(ccp(1.0f, 0.5f));
		_cursor->setPosition(ccp(_frame->getContentSize().width, _frame->getContentSize().height/2.f));
	}
	else
	{
		_label->setAnchorPoint(ccp(0.f, 0.5f));
		_label->setPosition(ccp(0.f, _frame->getContentSize().height/2.f));
		_cursor->setAnchorPoint(ccp(0.f, 0.5f));
		float fLbWidth = 0.f;
		std::wstring str = _label->getString();
		if (!str.empty())
			fLbWidth = _label->getContentSize().width*_label->getScaleX();
		_cursor->setPosition(ccp(fLbWidth, _frame->getContentSize().height/2.f));
	}
}

void CELabelMBFontWrapScis_cursor::updateLabelPosition()
{
	float fLbWidth = 0.f;
	std::wstring str = _label->getString();
	if (!str.empty())
		fLbWidth = _label->getContentSize().width*_label->getScaleX();

	switch (_ta)
	{
	case eCE_TA_left:
		{
			_label->setAnchorPoint(ccp(0.f, 0.5f));
			_label->setPosition(ccp(0.f, _frame->getContentSize().height/2.f));
			_cursor->setAnchorPoint(ccp(0.f, 0.5f));
			_cursor->setPosition(ccp(fLbWidth, _frame->getContentSize().height/2.f));
			break;
		}
	case eCE_TA_center:
		{
			_label->setAnchorPoint(ccp(0.5f, 0.5f));
			_label->setPosition(ccp((_frame->getContentSize().width - _cursor->getContentSize().width*_cursor->getScaleX())/2.f, _frame->getContentSize().height/2.f));
			_cursor->setAnchorPoint(ccp(0.f, 0.5f));
			_cursor->setPosition(ccp((_frame->getContentSize().width - _cursor->getContentSize().width*_cursor->getScaleX() + fLbWidth)/2.f,
									 _frame->getContentSize().height/2.f));
			break;
		}
	case eCE_TA_right:
		{
			_label->setAnchorPoint(ccp(1.0f, 0.5f));
			_label->setPosition(ccp(_frame->getContentSize().width - _cursor->getContentSize().width*_cursor->getScaleX(), _frame->getContentSize().height/2.f));
			_cursor->setAnchorPoint(ccp(1.0f, 0.5f));
			_cursor->setPosition(ccp(_frame->getContentSize().width, _frame->getContentSize().height/2.f));
			break;
		}
	case eCE_TA_left_to_right:
		{
			updateLeftToRightAllign();
			break;
		}
	};
}


}; // end namespace cocosext

